<?php
/**
 * DYSHOP
 * ============================================================================
 * 版权所有 2005-2028 杭州鼎易信息科技有限公司，并保留所有权利。
 * 网站地址: https://www.doing.net.cn
 * ==========================================
 * 本系统由鼎易科技团队开发 156791994@qq.com
 * ============================================================================
 * $Author: TANGYIN 2020-06-06 $
 *
 */

namespace app\admin\controller;
use think\facade\View;

use think\facade\Db;


use app\common\model\Users as UsersModel;
use app\common\model\Region as RegionModel;
use app\admin\validate\Users as validateUsers;

class Users extends Base
{

    function list(){
        if(IS_AJAX){
            $model = new UsersModel();
            $params = input('param.');
            $res = $model->tableData($params);
            return dyajaxReturn(1,'获取成功',$res['data'],'',$res['count']);
        }else {

            return View::fetch();
        }
    }

    public function add(){
        $param = input('param.');
        if(IS_POST){
            $validate = new validateUsers;
            $result = $validate->scene('add')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }

            $model = new UsersModel();

            $RegionModel = new RegionModel();
            $pro_one = $RegionModel -> where("id","=",$param['province_id'])->find();
            $param['province'] = isset($pro_one['name'])?$pro_one['name']:'';
            $city_one = $RegionModel -> where("id","=",$param['city_id'])->find();
            $param['city'] = isset($city_one['name'])?$city_one['name']:'';
            $area_one = $RegionModel -> where("id","=",$param['area_id'])->find();
            $param['area'] = isset($area_one['name'])?$area_one['name']:'';

            $res = $model -> handle($param);

            return dyajaxReturn($res['code'],$res['msg']);
        }else {
            $RegionModel = new RegionModel();
            $shen_list = $RegionModel
                ->where("parent_id","=",0)
                ->where("level","=",1)
                ->select();
            //dump($shen_list);
            View::assign('shen_list',$shen_list);
            return View::fetch();
        }
    }


    public function edit(){
        $param = input('param.');
        $model = new UsersModel();
        if(IS_POST){
            $param = input('param.');

            $validate = new validateUsers;
            $result = $validate->scene('edit')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }
            $res = $model -> handle($param);
            return dyajaxReturn($res['code'],$res['msg']);
        }else{
            $id = $param['id'];
            $info = $model->where('id','=',$id)->find();
            $info['picurl'] = explode(",",$info['picurl']);

            View::assign('id',$id);
            View::assign('info',$info);

            return View::fetch();
        }
    }

    public function shenhe(){
        $param = input('param.');
        $model = new UsersModel();
        if(IS_POST){
            $param = input('param.');
            $id = $param['id'];

            $res = $model->where("id","=",$id)->update([
                'status'=>$param['status'],
                'status_reply'=>$param['status_reply']
            ]);
            if(!$res){
                return dyajaxReturn(0,'提交失败');
            }
            return dyajaxReturn(1,'提交成功');
        }else{
            $id = $param['id'];
            $info = $model->where('id','=',$id)->find();

            $RegionModel = new RegionModel();
            $shen_list = $RegionModel
                ->where("parent_id","=",0)
                ->where("level","=",1)
                ->select();
            View::assign('shen_list',$shen_list);

            //城市遍历
            $city_list = $RegionModel
                ->where("parent_id","=",$info['province_id'])
                ->where("level","=",2)
                ->select();
            View::assign('city_list',$city_list);

            //区遍历
            $area_list = $RegionModel
                ->where("parent_id","=",$info['city_id'])
                ->where("level","=",3)
                ->select();
            View::assign('area_list',$area_list);

            View::assign('id',$id);
            View::assign('info',$info);

            return View::fetch();
        }
    }

    public function shenhevip(){
        $param = input('param.');
        $model = new UsersModel();
        if(IS_POST){
            $param = input('param.');
            $id = $param['id'];

            $res = $model->where("id","=",$id)->update([
                'is_vip'=>$param['is_vip'],
                'is_vip_reply'=>$param['is_vip_reply']
            ]);
            if(!$res){
                return dyajaxReturn(0,'提交失败');
            }
            return dyajaxReturn(1,'提交成功');
        }else{
            $id = $param['id'];
            $info = $model->where('id','=',$id)->find();

            $RegionModel = new RegionModel();
            $shen_list = $RegionModel
                ->where("parent_id","=",0)
                ->where("level","=",1)
                ->select();
            View::assign('shen_list',$shen_list);

            //城市遍历
            $city_list = $RegionModel
                ->where("parent_id","=",$info['province_id'])
                ->where("level","=",2)
                ->select();
            View::assign('city_list',$city_list);

            //区遍历
            $area_list = $RegionModel
                ->where("parent_id","=",$info['city_id'])
                ->where("level","=",3)
                ->select();
            View::assign('area_list',$area_list);

            View::assign('id',$id);
            View::assign('info',$info);

            return View::fetch();
        }
    }

    //删除
    public function delete(){
        $param = input('param.');
        $table = $param['table'];
        $id_name = $param['id_name'];
        $id = $param['id'];

        $res = DB::name($table)->where($id_name,'=',$id)->delete();
        if(!$res){
            return dyajaxReturn(0,'删除失败');
        }
        return dyajaxReturn(1,'删除成功');
    }


    //状态改变
    public function changeStatus(){
        $param = input('param.');
        $table = $param['table'];
        $id_name = $param['id_name'];
        $id = $param['id'];
        $field = $param['field'];
        $checked = $param['checked'];

        if($checked == 'true'){
            $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>1]);
        }else{
            $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>2]);
        }
        if(!$res){
            return dyajaxReturn(0,'操作失败');
        }
        return dyajaxReturn(1,'操作成功');
    }


}